package com.example.administrator.capital.mine.second;


import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.utils.ToastUtil;
import com.example.administrator.capital.widget.CountDownTimerUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * 绑定手机号
 */
public class BindPhoneFragment extends BaseBackFragment {
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.iv_img_code)
    ImageView mIvImgCode;
    @BindView(R.id.et_img_code)
    EditText mEtImgCode;
    @BindView(R.id.tv_send_code)
    TextView mTvSendCode;
    @BindView(R.id.point)
    View mPoint;
    @BindView(R.id.et_phone_code)
    EditText mEtPhoneCode;
    @BindView(R.id.btn_ok)
    Button mBtnOk;
    Unbinder unbinder;
    private TextView mBack;
    private String phone;

    public BindPhoneFragment() {
        // Required empty public constructor
    }

    public static BindPhoneFragment newInstance() {

        Bundle args = new Bundle();
        BindPhoneFragment fragment = new BindPhoneFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bind_phone, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mBack = view.findViewById(R.id.app_title_back);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();

            }
        });
        mTvSendCode.setOnClickListener(this);
        mBtnOk.setOnClickListener(this);

    }

    @Override
    protected void setData() {
        super.setData();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_send_code:
                if (isPhoneNumber()) {
                    CountDownTimerUtils utils = new CountDownTimerUtils(mTvSendCode, 60000, 1000);
                    utils.start();
                }
                break;
            case R.id.btn_ok:
                ToastUtil.showShort("绑定手机号");
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private boolean isPhoneNumber() {
        phone = mEtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getContext(), "手机号不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        } else if (CommonUtils.isMobileNO(phone)) {
            return true;
        } else {
            Toast.makeText(getContext(), "手机号不合法!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}
